package cn.lsnu.modules.oa.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.modules.oa.entity.RoleEntity;
import cn.lsnu.modules.oa.service.RoleService;
import cn.lsnu.utils.Global;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/oa/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/save")
    public void save(RoleEntity roleEntity) throws Exception{

        System.out.println(roleEntity);
        roleService.save(roleEntity);
    }

    @RequestMapping("/list")
    public String list(@RequestParam("menuId")String id, Model model) throws  Exception{

        model.addAttribute("pageId",id);
        return "/webpage/modules/oa/admin-role";
    }

    @RequestMapping("/listjson")
    public @ResponseBody Page<RoleEntity> listJson(
            RoleEntity roleEntity) throws  Exception{

        Page<RoleEntity> lists = roleService.findPage(roleEntity);
        return lists;
    }

    @RequestMapping("/updmsg")
    public String updMsg(String id,Model model) throws Exception{

        RoleEntity role = roleService.get(id);
        model.addAttribute("updrole",role);
        return "/webpage/modules/oa/admin-role-upd";
    }

    @RequestMapping("/upd")
    public void upd(RoleEntity roleEntity) throws Exception{
        roleService.update(roleEntity);
    }



    @RequestMapping("/del")
    public @ResponseBody
    Map<String,String> delete(RoleEntity roleEntity) throws Exception {
        roleService.delete(roleEntity);
        Map<String,String> mapJson = new HashMap<String,String>();
        mapJson.put("msg", Global.LOGIN_SUCCESS_STRING);
        return  mapJson;
    }


    @RequestMapping("/sea")
    public String search()throws Exception{

        return "";
    }
}
